package priv.dajie.tzzq.spider.csgo.buff;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import priv.dajie.tzzq.bot.BotManager;
import priv.dajie.tzzq.bot.buff.BuffUser;
import priv.dajie.tzzq.common.ApplicationContextUnit;
import priv.dajie.tzzq.service.PlatformCsgoService;
import priv.dajie.tzzq.spider.Base.Spider;
import priv.dajie.tzzq.spider.csgo.SpiderInfoElement;
import priv.dajie.tzzq.spider.csgo.SpiderInfos;
import priv.dajie.tzzq.spider.model.CsgoItem;
import priv.dajie.tzzq.spider.model.CsgoItemInfo;
import priv.dajie.tzzq.spider.model.PlatformCsgoItem;

import java.math.BigDecimal;
import java.net.URISyntaxException;

/**
 * @ClassName BuffCsgoSpider
 * @Description
 * @Author dajie
 * @Date 2019/11/10 8:54 上午
 * @Version V1.0
 **/
public class BuffCsgoSpider implements Spider, Runnable {

    private BuffUser buffUser;
    private SpiderInfos spiderInfos;
    private PlatformCsgoService buffCsgoService = (PlatformCsgoService) ApplicationContextUnit.getInstance().getBean("buffCsgoService");

    public BuffCsgoSpider(SpiderInfos spiderInfos) {
        this.spiderInfos = spiderInfos;
        buffUser = BotManager.getBuffSpdierBot().getBuffUser();
    }

    @Override
    public void run() {
        startSpider();
    }

    @Override
    public void startSpider() {
        while (!spiderInfos.isEmpty()){
            SpiderInfoElement spiderInfoElement = spiderInfos.getSpiderInfoElement();;
            System.out.println("剩余:"+spiderInfos.getSize() + " | "+ spiderInfoElement);
            getItem(spiderInfoElement);
        }
    }

    private void getItem(SpiderInfoElement spiderInfoElement){
        int page = 1;
        int pageSize= 300;
        String game = "csgo";
        while (true) {
            try {
                URIBuilder uriBuilder = new URIBuilder("https://buff.163.com/api/market/goods");
                uriBuilder.setParameter("page_num", String.valueOf(page));
                uriBuilder.setParameter("game", game);
                uriBuilder.setParameter(spiderInfoElement.getKey(), spiderInfoElement.getValue());
                uriBuilder.setParameter("page_size", String.valueOf(pageSize));
                String jsonStr="";
                try {
                    jsonStr = buffUser.open(new HttpGet(uriBuilder.build()));
                }
                catch (Exception e){
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException ex) {
                        ex.printStackTrace();
                    }
                }
                JSONObject json = JSON.parseObject(jsonStr);
                JSONObject data = json.getJSONObject("data");
                JSONArray items = data.getJSONArray("items");
                for (int i=0;i<items.size();i++){
                    JSONObject item = items.getJSONObject(i);
                    // 设置参数
                    CsgoItemInfo csgoItemInfo = new CsgoItemInfo();
                    csgoItemInfo.setSteamName(item.getString("market_hash_name"));
                    csgoItemInfo.setId(item.getString("id"));
                    csgoItemInfo.setName(item.getString("name"));
                    csgoItemInfo.setType(spiderInfoElement.getTypeName());
                    PlatformCsgoItem platformCsgoItem = new PlatformCsgoItem();
                    platformCsgoItem.setBPrice(item.getBigDecimal("buy_max_price"));
                    platformCsgoItem.setBNum(item.getInteger("buy_num"));
                    platformCsgoItem.setPrice(item.getBigDecimal("sell_min_price"));
                    platformCsgoItem.setNum(item.getInteger("sell_num"));
                    platformCsgoItem.setCsgoItemInfo(csgoItemInfo);
                    buffCsgoService.autoAddInfo(csgoItemInfo);
                    buffCsgoService.autoAddItem(platformCsgoItem);
                }
                page++;
                if (page*pageSize>=data.getInteger("total_count")){
                    break;
                }
            } catch (URISyntaxException e) {
                e.printStackTrace();
            }
        }
    }


    public static void main(String[] args) {
        BuffCsgoSpider buffCsgoSpider = new BuffCsgoSpider(BuffCsgoInfo.getSpiderInfos());
        buffCsgoSpider.startSpider();
    }
}
